﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Duet.Web.Core
{
    public class CreateTagsWebForm
    {
        #region Construtores

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceControl">Controle onde será adicionado a tag</param>
        /// <param name="initIndex">Número que representa a partir de qual indice as tag serão adicionadas.</param>
        public CreateTagsWebForm(Control sourceControl, int initIndex)
        {
            this.SourceControl = sourceControl;
            this._initIndex = initIndex;
        }

        #endregion

        #region Propriedades

        /// <summary>
        /// Controle onde será adicionado a tag
        /// </summary>
        public Control SourceControl { get; set; }

        #endregion

        #region Campos
        /// <summary>
        ///     Meta tags da página.
        /// </summary>
        private Dictionary<string, string> _metaTags = new Dictionary<string, string>();

        /// <summary>
        /// Número que representa a partir de qual indice as tag serão adicionadas.
        /// </summary>
        private int _initIndex;

        #endregion

        #region Métodos

        /// <summary>
        ///     Escreve as meta tags na página.
        /// </summary>
        public void WriteMetaTags()
        {
            foreach (var entry in _metaTags)
            {
                HtmlMeta meta = new HtmlMeta();
                meta.Name = entry.Key;
                meta.Content = entry.Value;

                this.SourceControl.Controls.Add(meta);
            }
        }

        /// <summary>
        ///     Adiciona caminhos de CSS à página.
        /// </summary>
        public void WriteLinkCSSTag(string path)
        {
            HtmlLink link = new HtmlLink();
            link.Href = path;
            link.Attributes.Add("rel", "stylesheet");
            link.Attributes.Add("type", "text/css");

            //this.SourceControl.Controls.AddAt(this._initIndex++, link);
            this.SourceControl.Controls.Add(link);
        }

        /// <summary>
        ///     Escreve caminhos de scripts na página.
        /// </summary>
        public void WriteScriptTag(string path)
        {
            HtmlGenericControl script = new HtmlGenericControl();
            script.TagName = "script";
            script.Attributes.Add("type", @"text/javascript");
            script.Attributes.Add("language", "javascript");
            script.Attributes.Add("src", this.SourceControl.Page.ResolveClientUrl(path));

            this.SourceControl.Controls.AddAt(this._initIndex++, script);
        }

        /// <summary>
        ///  Escreve a tag do Jquery
        /// </summary>
        public void WriteJqueryTag()
        {
            WriteScriptTag("~/util/Scripts/jquery.1.7.1.min.js");
        }

        /// <summary>
        /// Escreve a tag do Jquery UI
        /// </summary>
        public void WriteJqueryUiTag()
        {
            WriteScriptTag("~/util/Scripts/jquery-ui.1.8.16.min.js");
        }

        #endregion
    }
}
